package com.yangjun.utils;

import java.lang.reflect.Method;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class DvUtils {
	/**
	 * @Description 打乱list的顺序
	 * @param list
	 * @author yangjun
	 * @Date 2016年10月14日 上午10:30:41
	 */
	public static <T> List<T> randomList(List<T> list){
		Random random = new Random();
		for(int i=0,len=list.size();i<len;i++){
			int pos = random.nextInt(list.size());
			T tmp = list.get(i);
			list.set(i, list.get(pos));
			list.set(pos,tmp);
		}
		return list;
	}
	 /**
	  * @Description 判断页面类型
	  * @param pagetype
	  * @return
	  * @author yangjun
	  * @Date 2016年10月14日 上午10:33:22
	  */
	 public static String getPageType(String pagetype){
		 if("other".equals(pagetype)){
			 return "其他";
		 }else if("direct".equals(pagetype)){
			 return "直接访问";
		 }else if("information".equals(pagetype)){
			 return "资讯页";
		 }else if("category".equals(pagetype)){
			 return "分类页";
		 }else if("zhuanti".equals(pagetype)){
			 return "专题页";
		 }else if("index".equals(pagetype)){
			 return "首页";
		 }else if("product".equals(pagetype)){
			 return "商品详情页";
		 }else if("search".equals(pagetype)){
			 return "搜索结果页";
		 }else if("out".equals(pagetype)){
			 return "退出";
		 }else{
			 return pagetype;
		 }
	 }
	 
		/**
		 * 把一串方法名变成方法
		 * 
		 * @param clazz
		 * @param fields
		 * @param prefix
		 * @param postfix
		 * @return
		 */
		public static List<Method> getStringToMethodList(Class clazz,
				List<String> fields, String prefix, String postfix) {
			List<Method> list = new ArrayList<Method>();
			Method m = null;
			for (String fieldName : fields) {
				m = getStringToMethod(clazz, fieldName, prefix, postfix);
				list.add(m);
			}
			return list;
		}
		
		/**
		 * 把一个方法名变成方法
		 * 
		 * @param clazz
		 * @param fieldName
		 * @param prefix
		 * @param postfix
		 * @return
		 */
		public static Method getStringToMethod(Class clazz, String fieldName,
				String prefix, String postfix) {
			Method[] ms = clazz.getMethods();
			Method method = null;
			for (Method m : ms) {
				if (((prefix + fieldName + postfix)).trim().equalsIgnoreCase(
						m.getName())) {
					method = m;
					break;
				}
			}
			return method;
		}
		
		/**
		 * 是否是数字
		 * 
		 * @param sNumber
		 * @return
		 */
		public static boolean isNumber(String sNumber) {
			boolean isNumber = false;
			if (sNumber != null) {
				isNumber = sNumber
						.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
			}
			return isNumber;
		}
		
		public static void PinYinComparatorSortList(List<Object[]> resultList){
			Collections.sort(resultList,new PinYinComparatorSortList());
		}
		//自定义比较器
		static class PinYinComparatorSortList implements Comparator<Object[]>{
			@SuppressWarnings("rawtypes")
			Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
			@SuppressWarnings("unchecked")
			@Override
			public int compare(Object[] arr1, Object[] arr2) {
				String name1 = arr1[0].toString();
				String name2 = arr2[0].toString();
				return cmp.compare(name1,name2);

			}
		}
		/**
		 * 
		 * @Description 获取IP
		 * @param request
		 * @return
		 * @author yangjun
		 * @Date 2017年11月14日 上午11:44:06
		 */
		 public static String getIp(HttpServletRequest request) {
	         String ip = request.getHeader("X-Forwarded-For");
	         if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
	             //多次反向代理后会有多个ip值，第一个ip才是真实ip
	             int index = ip.indexOf(",");
	             if(index != -1){
	                 return ip.substring(0,index);
	             }else{
	                 return ip;
	             }
	         }
	         ip = request.getHeader("X-Real-IP");
	         if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
	             return ip;
	         }
	         return request.getRemoteAddr();
	     }

}
